package com.kfm.web;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.kfm.control.JdbcSelect;
import com.kfm.map.LogsTA;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/logsweb")
public class LogsWeb extends HttpServlet {
    @SneakyThrows
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //直接指定
        response.setContentType("text/html;charset=UTF-8");
        String sql = "select * from logsta";
        PrintWriter writer = response.getWriter();
        List<LogsTA> selectinfo = JdbcSelect.selectinfo(new LogsTA(), sql);
        //通过hultool创建JSON数组
        JSONArray array = JSONUtil.createArray();
        for (LogsTA logsTA : selectinfo) {
            //将对象转为JSON格式
            String jsonStr = JSONUtil.toJsonStr(logsTA);
            //添加到数组
            array.add(jsonStr);
        }
        writer.write(JSONUtil.toJsonStr(array));
    }
}
